const app = getApp();

export default {
  getSearchLabelByType, //获取筛选标签
  changeCity,
  changeArea,
  changeGym,
  submitArea,
  resetArea,
  changeTag,
  submitTag,
  resetTag,
  changeType,
  submitType,
  resetType,
  changeTime,
  submitTime,
  resetTime,
  changeOver,
};

function getSearchLabelByType() {
  const that = this;
  const tabId = that.data.tabId;
  const cities = that.data.cities || [];
  const regions = that.data.regions || [];
  const gymUuids = that.data.gymUuids || [];
  const labels = that.data.labels || [];
  const intervalTimes = that.data.intervalTimes || [];
  const genres = that.data.genres || [];

  app.middleware.requestAjax({
    serAddr: app.serAddrs.getSearchLabelByType,
    data: that.typeParams,
    cancelToast: true,
    success: function(res) {
      let screens = res.data;
      let temp = {},
        cityIdx = 0,
        areasIdx = 0;

      if (tabId == 5) {
        for (let a = 0; a < screens.length; a++) {

          // 选中城市显示
          cities.forEach((item, index) => {
            if (item == screens[a].city) {
              screens[a].screenStatus = true;
              cityIdx = a;
            }
          })

          // 选中市区显示
          for (let i = 0; i < screens[a].list.length; i++) {
            regions.forEach((item, index) => {
              if (item == screens[a].list[i].area) {
                screens[a].screenStatus = true;
                screens[a].list[i].screenStatus = true;
                areasIdx = i;
              }
            })

            // 选中健身房显示
            for (let j = 0; j < screens[a].list[i].list.length; j++) {
              gymUuids.forEach((item, index) => {
                if (item == screens[a].list[i].list[j].uuid) {
                  screens[a].screenStatus = true;
                  screens[a].list[i].screenStatus = true;
                  screens[a].list[i].list[j].screenStatus = true;
                  areasIdx = i;
                }
              })
            }
          }

        }

        if (cityIdx == 0) {
          screens[0].screenStatus = true;
        }

        temp.areas = screens;
        temp.cityIdx = cityIdx;
        temp.areasIdx = areasIdx;
      } else if (tabId == 4) {
        for (let i = 0; i < screens.length; i++) {
          for (let j = 0; j < screens[i].list.length; j++) {
            labels.forEach((item, index) => {
              if (item == screens[i].list[j].uuid) {
                screens[i].list[j].screenStatus = true;
              }
            })
          }
        }
        temp.tags = screens;
      } else if (tabId == 3) {
        screens = [screens];
        for (let i = 0; i < screens.length; i++) {
          for (let j = 0; j < screens[i].list.length; j++) {
            intervalTimes.forEach((item, index) => {
              if (item.id == screens[i].list[j].id) {
                screens[i].list[j].screenStatus = true;
              }
            })
          }
        }
        temp.times = screens;
      } else if (tabId == 2) {
        screens = [{
          name: "门店类型",
          list: screens
        }];
        for (let i = 0; i < screens.length; i++) {
          for (let j = 0; j < screens[i].list.length; j++) {
            genres.forEach((item, index) => {
              if (item == screens[i].list[j].id) {
                screens[i].list[j].screenStatus = true;
              }
            })
          }
        }
        temp.types = screens;
      }
      that.setData(temp);
    },
    fail: function(res) {

    }
  })
}

function changeCity(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  let areas = that.data.areas;
  const cityIdx = that.data.cityIdx || 0;

  areas.forEach(item => {
    item.screenStatus = false;
    item.list.forEach(childItem => {
      childItem.screenStatus = false;
    })
  });
  areas[index].screenStatus = !areas[index].screenStatus;

  that.setData({
    areas,
    cityIdx: index
  })
}

function changeArea(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  const cityIdx = that.data.cityIdx;
  let areas = that.data.areas;

  areas[cityIdx].list[index].screenStatus = !areas[cityIdx].list[index].screenStatus;

  that.setData({
    areas,
    areasIdx: index
  })
}

function changeGym(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  const cityIdx = that.data.cityIdx;
  const areasIdx = that.data.areasIdx;
  let areas = that.data.areas;

  areas[cityIdx].list[areasIdx].list[index].screenStatus = !areas[cityIdx].list[areasIdx].list[index].screenStatus;

  that.setData({
    areas,
  })
}

function submitArea() {
  const that = this;
  let areas = that.data.areas;
  let cities = [],
    regions = [],
    gymUuids = [];

  for (let i = 0; i < areas.length; i++) {

    // 城市选择
    if (areas[i].screenStatus) {
      cities.push(areas[i].city);
    }

    for (let j = 0; j < areas[i].list.length; j++) {

      // 市区选择
      if (areas[i].list[j].screenStatus) {
        regions.push(areas[i].list[j].area);
      }

      for (let k = 0; k < areas[i].list[j].list.length; k++) {

        // 健身房选择
        if (areas[i].list[j].list[k].screenStatus) {
          gymUuids.push(areas[i].list[j].list[k].uuid);
        }
      }

    }
  }

  that.listParams.cities = cities;
  that.listParams.areas = regions;
  that.listParams.tGymUuids = gymUuids;
  that.setData({
    cities,
    regions,
    gymUuids
  })
  // 初始化
  that.pageListInit();
}

function resetArea() {
  const that = this;
  let areas = that.data.areas;
  let regions = [],
    gymUuids = [],
    cityIdx = 0;

  for (let i = 0; i < areas.length; i++) {
    areas[i].screenStatus = false;
    for (let j = 0; j < areas[i].list.length; j++) {
      areas[i].list[j].screenStatus = false;
      for (let k = 0; k < areas[i].list[j].list.length; k++) {
        areas[i].list[j].list[k].screenStatus = false;
      }
    }
  }

  if (cityIdx == 0) {
    areas[0].screenStatus = true;
  }

  that.listParams.areas = regions;
  that.listParams.tGymUuids = gymUuids;
  that.setData({
    areas,
    cityIdx,
    regions,
    gymUuids
  })
  // 初始化
  that.pageListInit();
}

function changeTag(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  const idx = event.currentTarget.dataset.idx;
  let tags = that.data.tags;

  tags[index].list[idx].screenStatus = !tags[index].list[idx].screenStatus;

  that.setData({
    tags,
  })
}

function submitTag() {
  const that = this;
  let tags = that.data.tags;
  let labels = [];

  for (let i = 0; i < tags.length; i++) {
    for (let j = 0; j < tags[i].list.length; j++) {
      if (tags[i].list[j].screenStatus) {
        labels.push(tags[i].list[j].uuid)
      }
    }
  }

  that.listParams.tGoodsLabelNameUuids = labels;
  that.setData({
    labels,
  })
  // 初始化
  that.pageListInit();
}

function resetTag() {
  const that = this;
  let tags = that.data.tags;
  let labels = [];

  for (let i = 0; i < tags.length; i++) {
    for (let j = 0; j < tags[i].list.length; j++) {
      tags[i].list[j].screenStatus = false;
    }
  }

  that.listParams.tGoodsLabelNameUuids = labels;
  that.setData({
    tags,
    labels,
  })
  // 初始化
  that.pageListInit();
}

function changeType(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  const idx = event.currentTarget.dataset.idx;
  let types = that.data.types;

  types[index].list[idx].screenStatus = !types[index].list[idx].screenStatus;

  that.setData({
    types,
  })
}

function submitType() {
  const that = this;
  let types = that.data.types;
  let genres = [];

  for (let i = 0; i < types.length; i++) {
    for (let j = 0; j < types[i].list.length; j++) {
      if (types[i].list[j].screenStatus) {
        genres.push(types[i].list[j].id);
      }
    }
  }

  that.listParams.gymTypeIds = genres;
  that.setData({
    genres,
  })
  // 初始化
  that.pageListInit();
}

function resetType() {
  const that = this;
  let types = that.data.types;
  let genres = [];

  for (let i = 0; i < types.length; i++) {
    for (let j = 0; j < types[i].list.length; j++) {
      types[i].list[j].screenStatus = false;
    }
  }

  that.listParams.gymTypeIds = genres;
  that.setData({
    types,
    genres,
  })
  // 初始化
  that.pageListInit();
}

function changeTime(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  const idx = event.currentTarget.dataset.idx;
  let times = that.data.times;

  times[index].list[idx].screenStatus = !times[index].list[idx].screenStatus;

  that.setData({
    times,
  })
}

function submitTime() {
  const that = this;
  let times = that.data.times;
  let intervalTimes = [];

  for (let i = 0; i < times.length; i++) {
    for (let j = 0; j < times[i].list.length; j++) {
      if (times[i].list[j].screenStatus) {
        intervalTimes.push(times[i].list[j]);
      }
    }
  }

  that.listParams.intervalTimes = intervalTimes;
  that.setData({
    intervalTimes,
  })
  // 初始化
  that.pageListInit();
}

function resetTime() {
  const that = this;
  let times = that.data.times;
  let intervalTimes = [];

  for (let i = 0; i < times.length; i++) {
    for (let j = 0; j < times[i].list.length; j++) {
      times[i].list[j].screenStatus = false;
    }
  }

  that.listParams.intervalTimes = intervalTimes;
  that.setData({
    times,
    intervalTimes,
  })
  // 初始化
  that.pageListInit();
}

function changeOver() {
  const that = this;
  let temp = {};
  let isShadowOver = that.data.isShadowOver;
  temp.isShadowOver = !isShadowOver;
  that.listParams.isShadowOver = temp.isShadowOver ? 1 : 0;
  that.setData(temp);
  // 初始化
  that.pageListInit();
}